Skip to content

Method to keep objects in front - #840

Merged
ocefpaf merged 7 commits into
python-visualization:masterfrom
Conengmo:keep-in-front
May 7, 2018
Merged

ocefpaf merged 7 commits into
python-visualization:masterfrom
Conengmo:keep-in-front

Conversation

@Conengmo

@Conengmo Conengmo commented Apr 2, 2018 •

Copy link
Copy Markdown
Member

This is an approach to tackle #826. I wanted to make something so that a user can select an object that should always stay in front.

At first I thought of adding this as an argument when creating objects, but that would mean touching many classes and a lot of repeated code and docstrings. This would be most in line with how folium currently works, but I'm not sure if its worth it for a feature that may not be used that much.

Then I looked into adding a method to a base class such as Element, so a user can call it on any type of object. But that would mean adding Leaflet code to Branca, which I assume is not the purpose of Branca. Alternatively I could add a method to Layer in folium, but then it would not work for objects such as Marker.

So finally I went for adding this method to Map. This makes sense to me, a user is telling the map which objects it wants to keep in front. It's a separate method, so it shouldn't confuse new users, but its there if someone needs it.

Here's a code snippet to test the functionality:

import folium
m = folium.Map((0, 0), zoom_start=8)
coords = [[[-1, 1], [-1, -1], [1, -1], [1, 1], [-1, 1]]]
fgs = {}
for c in ['blue', 'green', 'red']:
    fgs[c] = folium.FeatureGroup(name=c).add_to(m)
    folium.Polygon(coords, color=c, fill_color=c, popup=c).add_to(fgs[c])
m.keep_in_front(fgs['green'], fgs['blue'])
folium.LayerControl(collapsed=False).add_to(m)
m.save('_map.html')

Comment thread folium/folium.py Outdated

def keep_in_front(self, *args):
"""Pass one or multiples object that must stay in front.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

Comment thread folium/folium.py Outdated
"""Pass one or multiples object that must stay in front.

The ordering matters, the last one is put on top.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

Comment thread folium/folium.py Outdated

Parameters
----------
*args :

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Comment thread folium/folium.py Outdated
Parameters
----------
*args :
Variable length argument list. Any folium object that counts as an

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Comment thread folium/folium.py Outdated
for obj in args:
self.objects_to_stay_in_front.append(obj)


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W391 blank line at end of file

@ocefpaf

ocefpaf commented May 7, 2018

Copy link
Copy Markdown
Member

@Conengmo this is a simple and elegant solution! Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants